package com.ruoyi.web.controller.admin;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.dto.XsUserDto;
import com.ruoyi.system.domain.vo.user.XsUserVo;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.IXsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * TB 订单表Controller
 */
@RestController
@RequestMapping("/system/xs/user")
public class XsUserController extends BaseController {

    @Autowired
    private IXsUserService xsUserService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private TokenService tokenService;


    /**
     * 查询用户列表
     */
    @GetMapping("/list")
    public TableDataInfo appList(XsUserDto userDto) {
        startPage();
        return getDataTable(xsUserService.selectXsUserList(userDto));
    }

    /**
     * 注册用户/普通用户/客服/写手
     * @param userDto
     * @return
     */
    @PostMapping()
    public AjaxResult add(@Valid @RequestBody XsUserDto userDto) {
        return success(xsUserService.add(userDto));
    }

    /**
     * 导出用户列表
     */
    @Log(title = "导出用户列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, XsUserDto xsUserDto)
    {
        List<XsUserVo> list = xsUserService.selectXsUserList(xsUserDto);
        ExcelUtil<XsUserVo> util = new ExcelUtil<XsUserVo>(XsUserVo.class);
        util.exportExcel(response, list, "导出用户列表");
    }

    /**
     * 禁用用户（可批量）
     * @param ids
     * @return
     */
    @Log(title = "用户禁用", businessType = BusinessType.UPDATE)
    @PutMapping("/disable/{ids}")
    public AjaxResult disable(@PathVariable Long[] ids) {
        return success(xsUserService.disableUser(ids));
    }


    /**
     * 获取用户详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(xsUserService.getUserInfo(id));
    }

    /**
     * 修改用户信息
     */
    @Log(title = "修改用户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody XsUserDto xsUserDto) {
        return toAjax(xsUserService.updateXsUser(xsUserDto));
    }

}